package com.aim.Service;

import java.io.File;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.aim.Activity.ImpoundTab;
import com.aim.Activity.R;
import com.aim.api.AIMCaseConstants;
import com.aim.util.FileUploader;
import com.aim.util.UIUtilities;

public class UploadPicService extends Service {
	private static final String TAG = "UploadPicService";
	private NotificationManager _nm;
//	int picNo;
	String caseNo;
	@Override
	public IBinder onBind(Intent i) {
	Log.e(TAG, "============> TestService.onBind");
	return null;
	}

	public class LocalBinder extends Binder {
	public UploadPicService getService() {
	return UploadPicService.this;
	}
	}

	@Override
	public boolean onUnbind(Intent i) {
	Log.e(TAG, "============> TestService.onUnbind");
	return false;
	}

	@Override
	public void onRebind(Intent i) {
	Log.e(TAG, "============> TestService.onRebind");
	}

	@Override
	public void onCreate() {
	Log.e(TAG, "============> TestService.onCreate");
	_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onStart(Intent intent, int startId) {
	Log.e(TAG, "============> TestService.onStart");

	try {
		Bundle extras = intent.getExtras();        
		if (extras != null) {
			caseNo = extras.getString("caseno"); 
			new Thread(){  
				  
		        @Override  
		        public void run() {  
		        	for(int i =1; i<=ImpoundTab.LIMIT; i++)
		        	{ 
		        		String picname= AIMCaseConstants.SAVEDDIRECTORY+caseNo+"img"+i+".jpg";
		        	    File mF = new File(picname);
		        	    if(mF.exists())
		        	    { 
		        	    	uploadImage(i);
		        	    	mF.delete();
		        	    }
		        	 
		        	}
//                handler.sendEmptyMessage(2);  
		            
		        }}.start();

			
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	onDestroy();
	}

	@Override
	public void onDestroy() {
		
	_nm.cancel(null, 0);
	Log.e(TAG, "============> TestService.onDestroy");
	}
	
	private void uploadImage(int i) {
		
		String pictureFileName = AIMCaseConstants.SAVEDDIRECTORY+caseNo+"img"+i+".jpg";
//		String
		FileUploader uploader = new FileUploader();
		File f = new File(pictureFileName);
		if (f.exists())
		{
			boolean result = uploader.doFileUpload(pictureFileName, caseNo);
		if (result) {
//			UIUtilities.showFormattedToast(getApplicationContext(),
//					R.string.success_added_image, pictureFileName);
			Log.e("result1",pictureFileName);
		
		} else {
//			UIUtilities.showFormattedToast(getApplicationContext(),
//					R.string.fail_added_image, pictureFileName);
			Log.e("result0",pictureFileName);
		
		}}
		
	}

	}